/*package com.redis.base.redis;

import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.ibatis.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;

import com.redis.base.config.RedisParamConfig;

import redis.clients.jedis.exceptions.JedisConnectionException;

public class MybatisRedisCache implements Cache {

	private static final Logger log = LoggerFactory.getLogger(MybatisRedisCache.class);

	private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

	private final String id;
	
	private static RedisParamConfig redisConfig;

	static RedisTemplate<Object, Object> redisTemplate;

	public MybatisRedisCache(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	@Override
	public void clear() {
		try {
			if (!isEnable()) {
				return;
			}
			Set<Object> keys = redisTemplate.keys("*" + this.id + "*");
			if (!CollectionUtils.isEmpty(keys)) {
				redisTemplate.delete(keys);
			}
		} catch (JedisConnectionException e) {
			log.error(e.getMessage());
		}
	}
	
	public void deleteLike(String ... keys) {
		try {
			if (!isEnable()) {
				return;
			}
			for(String key : keys) {
				Set<Object> keysSet = redisTemplate.keys("*" + key + "*");
				for(Object value : keysSet) {
					redisTemplate.delete(value);
				}
			}
		} catch (JedisConnectionException e) {
			log.error(e.getMessage());
		}
	}

	@Override
	public void putObject(Object key, Object value) {
		if (!isEnable()) {
			return;
		}
		try {
			redisTemplate.opsForValue().set(replaceBlank(String.valueOf(key)), value,redisConfig.getExpire(),TimeUnit.SECONDS);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	@Override
	public Object getObject(Object key) {
		if (!isEnable()) {
			return null;
		}
		try {
			return redisTemplate.opsForValue().get(replaceBlank(String.valueOf(key)));
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}
	}

	private String replaceBlank(String str) {
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	@Override
	public Object removeObject(Object key) {
		if (!isEnable()) {
			return null;
		}
		redisTemplate.delete(key.toString());
		return true;

	}

	@Override
	public int getSize() {
		if (!isEnable()) {
			return 0;
		}
		redisTemplate.execute(new RedisCallback<Object>() {
			@Override
			public Object doInRedis(RedisConnection connection) {
				return Integer.valueOf(connection.dbSize().toString());
			}
		});
		return 0;
	}

	@Override
	public ReadWriteLock getReadWriteLock() {
		return this.readWriteLock;
	}

	public static void setRedisTemplates(RedisTemplate<Object, Object> redisTemplate) {
		MybatisRedisCache.redisTemplate = redisTemplate;
	}

	public boolean isEnable() {
		return redisConfig.isEnable();
	}


	public static RedisParamConfig getRedisConfig() {
		return redisConfig;
	}

	public static void setRedisConfig(RedisParamConfig redisConfig) {
		MybatisRedisCache.redisConfig = redisConfig;
	}

	
}
*/